(function() {
	angular
		.module('ydApp')
		.directive('calendar', function($timeout) {
			return {
				restrict: "EA",
				templateUrl: 'directive/calendar/calendar.html',
				replace: true,
				scope: {
					list: "=",
					date: "="
				},
				controller: function($scope, bdCalendar) {
					$scope.bdCalendar = bdCalendar;
				},
				link: function($scope, elem, attr) {
					var nowDate = new Date();
					var date = new Date();
					$scope.swipeLeft = function () {
						if (date.getMonth() - nowDate.getMonth() < 1) {
							console.log('swipeLeft');
							date.setMonth(date.getMonth()+1);
							init(date);
						}
					}
					$scope.swipeRight = function () {
						if (nowDate.getMonth() - date.getMonth() < 1) {
							console.log('swipeRight');
							date.setMonth(date.getMonth()-1);
							init(date);
						}
					}
					
					function init(now) {
						$timeout(function() {
							
							$scope.bdCalendar.init(now, function(date) {
								console.log(date)
								$(".today").removeClass('today');
								$("[data-id=" + date + "]").addClass("today");
								$scope.$apply(function() {
									$scope.date = date;
								})
							});
							if ($scope.list) {
								for (var i = 0; i < $scope.list.length; i++) {
									var day = new Date($scope.list[i]);
									var dataId = day.getFullYear() + '-' + (day.getMonth() + 1) + '-' + day.getDate();
									if (day.getDate() < now.getDate()) {
										$("[data-id=" + dataId + "]").addClass('day-yz before');
									} else {
										$("[data-id=" + dataId + "]").addClass('day-yz');
									}
								}
							}
							var loc = 7 - $("#cm>.week-row:first-child>.cell-num").length;
							$scope.ml = (loc * 14.2) + '%';
							$scope.month = now.getMonth() + 1;
						}, 100)
					}
					$scope.$watch('list', function () {
						init(date);
					}, true)
					

				}
			};
		})
})();